const { add, getCount, query, remove } = require('../../utils/dbTools')
const tableName = 'comment'
const memberTableName = 'member'

/**
 * 获取数据总条数
 * 
*/
const commentTotal = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { doctor_id, author_id } = req.query
    if (!doctor_id && !author_id) {
        output.code = 500
        output.msg = "参数错误:缺少doctor_id或者author_id, 必须传递其中之一"
        return res.send(output)
    }
    const map = {}
    // 查询条件
    doctor_id ? map.doctor_id = doctor_id : map.author_id = author_id
    const result = await getCount(tableName, map)
    if (!result) {
        output.code = 500
        output.msg = '操作失败'
    }
    output.data = result
    res.send(output)
}

/**
 *  根据医生id||患者id获取评论列表
 * 
*/
const commentList = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { doctor_id, author_id, page, size } = req.query
    if (!doctor_id && !author_id) {
        output.code = 500
        output.msg = "参数错误:缺少doctor_id和author_id, 必须提供其中之一"
        return res.send(output)
    }
    let sql = `select c.id,c.author_id,c.doctor_id,c.status,c.content,c.rate,c.create_time,s.name as patient`
    sql += ` from ${tableName} c inner join ${memberTableName} s on c.author_id=s.id`
    sql += ` where c.status=1`
    if (doctor_id) {
        sql += ` and c.doctor_id=${doctor_id}`
    } else {
        sql += ` and c.author_id=${author_id}`
    }
    sql += ` order by c.id desc`
    // 如果传递了分页参数, 则实现分页查询
    if (page && size) {
        sql += `  limit ${(page - 1) * size},${size}`
    }
    const result = await query(sql)
    if (!result) {
        output.code = 500
        output.msg = "操作失败"
        return res.send(output)
    }
    output.data = result
    res.send(output)
}

/**
 * 新增评论信息
 * 
*/

const commentAdd = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    let { doctor_id, rate, content } = req.body
    if (!doctor_id || !content || !rate) {
        output.code = 500
        output.msg = "参数错误: 缺少doctor_id,rate,content"
        return res.send(output)
    }
    // 登录账号id
    const { id: author_id } = req.loginInfo
    if (author_id === doctor_id) {
        output.code = 500
        output.msg = 'author_id不能和doctor_id相同'
        return res.send(output)
    }
    const data = {
        author_id, doctor_id, content, rate, create_time: Date.now()
    }
    const result = await add(data, tableName)
    if (!result) {
        output.code = 500
        output.msg = '操作失败'
        return res.send(output)
    }
    output.data = result
    res.send(output)
}

/**
 *  删除评论信息
 * 
*/

const commentRemove = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { id } = req.query
    if (!id) {
        output.code = 500
        output.msg = "参数错误"
        return res.send(output)
    }
    // const result = await update(id, { status: -1 }, tableName)
    const result = await remove(id, tableName)
    if (!result) {
        output.code = 500
        output.msg = "操作失败"
        return res.send(output)
    }
    res.send(output)
}

module.exports = {
    commentAdd,
    commentRemove,
    commentList,
    commentTotal
}